package com.kg.sdk;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import com.kg.sdk.activity.KPLoginActivity;
import com.kg.sdk.activity.KPUserCenterActivity;
import com.kg.sdk.util.CommonUtil;
import com.kg.sdk.util.KPNetworkData;

import static android.content.Context.ACTIVITY_SERVICE;

public class KPGameSdk {
    public static void showFloatView(Context context){
        if(isRunService(context)){
            Intent intent = new Intent(KPNetworkData.SHOW_FLOAT_VIEW);
            context.sendBroadcast(intent);
        }else{
            startFloatService(context);
        }

    }

    public static void hideFloatView(Context context){
       // Intent intent = new Intent(KPNetworkData.HIDE_FLOAT_VIEW);
      //  context.sendBroadcast(intent);
        stopFloatService(context);
    }

    public static void showFloatView1(Context context){
        Intent intent = new Intent(KPNetworkData.SHOW_FLOAT_VIEW);
        context.sendBroadcast(intent);

    }

    public static void hideFloatView1(Context context){
         Intent intent = new Intent(KPNetworkData.HIDE_FLOAT_VIEW);
          context.sendBroadcast(intent);
    }

    public static void startFloatService(Context context){
        Intent intent = new Intent(context, KPSDKGameMenuServer.class);
        context.startService(intent);
    }

    public static void stopFloatService(Context context){
        Intent intent = new Intent(context,KPSDKGameMenuServer.class);
        context.stopService(intent);
    }

    public static  boolean isLogin(Context context){
        SharedPreferencesCtrl sharedPreferencesCtrl = new SharedPreferencesCtrl(context,KPNetworkData.KPGAME);
        boolean is = sharedPreferencesCtrl.getBoolean(KPNetworkData.IS_LOGIN_KEY,false);
        return is;
    }

    public static void startLogin(Activity activity){
        if(isLogin(activity)){
            CommonUtil.showToast(activity,"已经登录了");
            return;
        }
        Intent intent = new Intent();
        intent.setClass(activity, KPLoginActivity.class);
        activity.startActivityForResult(intent,KPNetworkData.LOGIN_REQUEST);
    }

    public static void exitLogin(Context context){
        SharedPreferencesCtrl sharedPreferencesCtrl = new SharedPreferencesCtrl(context,KPNetworkData.KPGAME);
        sharedPreferencesCtrl.put(KPNetworkData.IS_LOGIN_KEY,false);
        hideFloatView(context);
        CommonUtil.showToast(context,"注销成功");
    }

    public final static String serviceName = "com.kg.sdk.KPSDKGameMenuServer";
    private static boolean isRunService(Context context) {
        ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceName.equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取自启动管理页面的Intent
     * @param context context
     * @return 返回自启动管理页面的Intent
     * */
    public static Intent getAutostartSettingIntent(Context context) {
        ComponentName componentName = null;
        String brand = Build.MANUFACTURER;
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        switch (brand.toLowerCase()) {
            case "samsung"://三星
                componentName = new ComponentName("com.samsung.android.sm", "com.samsung.android.sm.app.dashboard.SmartManagerDashBoardActivity");
                break;
            case "huawei"://华为
                //荣耀V8，EMUI 8.0.0，Android 8.0上，以下两者效果一样
                componentName = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity");
//            componentName = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity");//目前看是通用的
                break;
            case "xiaomi"://小米
                componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity");
                break;
            case "vivo"://VIVO
//            componentName = new ComponentName("com.iqoo.secure", "com.iqoo.secure.safaguard.PurviewTabActivity");
                componentName = new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity");
                break;
            case "oppo"://OPPO
//            componentName = new ComponentName("com.oppo.safe", "com.oppo.safe.permission.startup.StartupAppListActivity");
                componentName = new ComponentName("com.coloros.oppoguardelf", "com.coloros.powermanager.fuelgaue.PowerUsageModelActivity");
                break;
            case "yulong":
            case "360"://360
                componentName = new ComponentName("com.yulong.android.coolsafe", "com.yulong.android.coolsafe.ui.activity.autorun.AutoRunListActivity");
                break;
            case "meizu"://魅族
                componentName = new ComponentName("com.meizu.safe", "com.meizu.safe.permission.SmartBGActivity");
                break;
            case "oneplus"://一加
                componentName = new ComponentName("com.oneplus.security", "com.oneplus.security.chainlaunch.view.ChainLaunchAppListActivity");
                break;
            case "letv"://乐视
                intent.setAction("com.letv.android.permissionautoboot");
            default://其他
                intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                intent.setData(Uri.fromParts("package", context.getPackageName(), null));
                break;
        }
        intent.setComponent(componentName);
        return intent;
    }

    public static void startAutostartSetting(Context context){
        Intent intent = getAutostartSettingIntent(context);
        try{
            context.startActivity(intent);
        }catch (ActivityNotFoundException e){
        }

    }


    public static void startUserCenter(Context context){//启动用户中心
        if(!isLogin(context)){
            CommonUtil.showToast(context,"请先登录");
            return;
        }
        Intent intent = new Intent();
        intent.setClass(context, KPUserCenterActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    //获取手机类型
    private static String getMobileType() {
        return Build.MANUFACTURER;
    }
  
}
